package com.usian.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRealnameService;
import com.usian.user.service.ApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    @Autowired
    private ApUserRealnameService apUserRealnameService;

    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Autowired
    private WmUserFeign wmUserFeign;




    @Override
    @GlobalTransactional/*ap端用户申请为自媒体用户*/
    public ResponseResult updateStatusById(AuthDto dto) {

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /*传过来的状态不是2(审核不通过)也不是9(审核通过)就不执行后面代码*/
        if (dto.getStatus()!=2 && dto.getStatus()!=9){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        /*更新用户认证状态 看看是2还是9*/
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if (dto.getMsg()!=null && dto.getStatus() ==2){
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealnameService.updateById(apUserRealname);

        /*同步数据 添加自媒体和作者 */
        /*查询用户认证表*/
        ApUserRealname UserRealname = apUserRealnameService.getById(dto.getId());
        /*查询ap端用户表*/
        ApUser apUser = this.getById(UserRealname.getUserId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (dto.getStatus()==9) {
            /*保存自媒体 判断存在不存在  存在就保存*/
            WmUser wmUser = wmUserFeign.findByName(apUser.getName());
            /*保存作者*/
            ApAuthor apAuthor = apAuthorFeign.findByUserId(apUser.getId());

            if (wmUser == null) {
                wmUser = new WmUser();
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setStatus(9);  /*0 暂时不可用 1 永久不可用 9 正常可用*/
                wmUser.setPhone(apUser.getPhone());
                wmUserFeign.save(wmUser);
            }
            if (apAuthor==null){
                apAuthor = new ApAuthor();
                apAuthor.setName(wmUser.getName());
                apAuthor.setType(2);     /*0 爬取数据1 签约合作商2 平台自媒体人*/
                apAuthor.setCreatedTime(new Date());
                apAuthor.setUserId(apUser.getId());

                apAuthor = apAuthorFeign.save(apAuthor);
            }

            /*在添加作者时 查询自媒体 因为要添加 作者里面自媒体ID*/
            WmUser newWmUser = wmUserFeign.findByName(apUser.getName());
            apAuthor.setWmUserId(newWmUser.getId());
            apAuthorFeign.updateById(apAuthor);

            /*在自媒体里面添加作者ID   查询作者*/
            ApAuthor newApAuthor = apAuthorFeign.findByUserId(apUser.getId());
            newWmUser.setApAuthorId(newApAuthor.getId());
            wmUserFeign.updateById(newWmUser);

            /*更新 ap_user flag*/
            apUser.setFlag((short)1);/* 0 普通用户 1 自媒体人 2 大V*/
            this.updateById(apUser);

           /* int i = 1/0;*/    //测试全局事务

            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        /*更新 ap_user flag*/
        apUser.setFlag((short)0);/* 0 普通用户 1 自媒体人 2 大V*/
        this.updateById(apUser);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
